<?php 
	namespace App\Http\Controllers\Service;
	use App\Http\Controllers\Controller;
	//引入状态输出类 
	use App\Models\Result;
	use Redis;
	use App\Models\UserHasLoginRedis;
	use App\Models\PushMessageNum;
	use Illuminate\Http\Request;
	use App\Entity\User;
	use App\Entity\Wisdom;
	use App\Entity\TiezeDetail;
	use App\Entity\TiezeDetailComment;
	use App\Entity\Tieze;
	use App\Entity\Follow;
	use App\Entity\GoodTieze;
	use App\Entity\Reply;
	use App\Tool\ValidateCode;
	class MemberController extends Controller{
		public $imageBasePath='http://47.95.9.204:9501/';
		//检测是否登录 是登录状态就返回用户id否则返回0
		public function checkLogin($access_token){
			if($access_token){
				$userLoginStatus=new UserHasLoginRedis($access_token);
				return $userLoginStatus->uid;				
			}else{
				return 0;
			} 
		}
		public function getUserInfo(Request $request){
			$result=new Result();
			$access_token=$request->input('access_token','');
			if($access_token){
				$userLoginStatus=new UserHasLoginRedis($access_token);
				$result->status=$userLoginStatus->status;
			}else{
				$result->status=0;
			}
			if(!$result->status){
				$result->data='';
			}else{
				$user=User::find($userLoginStatus->uid);
				$result->data=$user;
			}
			return $result->toJson();
		}
		public function getUserInfoTiezeData(Request $request){
			$result=new Result();
			$access_token=$request->input('access_token','');
			//概览标识
			$isPreview=$request->input('isPreview','');
			$userLoginStatus=new UserHasLoginRedis($access_token);
			$uid=$request->input('uid','');
			$result->status=$userLoginStatus->status;
			if(!$userLoginStatus->status&&!$uid){
				$result->data='';
			}else{
				$uid=$uid?$uid:$userLoginStatus->uid;
				//三条就行
				if($isPreview=='yes'){
					//预览接口出现了用户id就代表请求这个接口的是user页不是member页
					//user接口要查用户的基本信息和当前用户的关注情况
					if($request->input('uid','')){
						$userInfo=User::where('id',$uid)->first();
						$result->data['userInfo']=$userInfo;						
						$result->data['userInfo']->isFollow=Follow::where('fid',$uid)->where('uid',$userLoginStatus->uid)->pluck('uid')?true:false;						
						$userFollowId=explode(',',$userInfo->followtieze);		
					}else{
						$userFollowId=explode(',',User::where('id',$uid)->pluck('followtieze'));
					}
					//关注的窝子
					$qz=Tieze::whereIn('id',$userFollowId)->get();
					$result->data['qz']=$qz;
					//最近发表的
					$recent_topics=(TiezeDetail::where('uid',$uid)->select('id','preview_content','comment','time','tid')->where('parent',0)->orderBy('time','desc')->paginate(3)->toArray())['data'];
					foreach ($recent_topics as $key => $value) {
						$recent_topics[$key]['partQz']=Tieze::where('id',$value['tid'])->pluck('name');
						$recent_topics[$key]['time']=$this->time_format($value['time']);
					}
					$result->data['recent_topics']=$recent_topics;

					//最近参与的回复(副贴 是回复也是贴子同时插入两个表)
					$recent_reply=(TiezeDetailComment::where('uid',$uid)->select('preview_content','time','tid','reply_uid')->orderBy('time','desc')->paginate(3)->toArray())['data'];
					foreach ($recent_reply as $key => $value) {	
						//取到贴子id
						$tiezeInfo=TiezeDetail::where('id',$value['tid'])->select('tid','parent','preview_content','comment')->first();
						$recent_reply[$key]['comment']=$tiezeInfo->comment;
						$recent_reply[$key]['id']=$value['tid'];
						$recent_reply[$key]['tid']=$tiezeInfo->tid;
						//存在上级标识是副贴
						if($tiezeInfo->parent){
							$recent_reply[$key]['content_child']=$tiezeInfo->preview_content;
							$recent_reply[$key]['content']=TiezeDetail::where('id',$tiezeInfo->parent)->pluck('preview_content');
						}else{	
							$recent_reply[$key]['content']=$tiezeInfo->preview_content;
							$recent_reply[$key]['content_child']='';
						}
						//取到贴子名字
						$recent_reply[$key]['partQz']=Tieze::where('id',$tiezeInfo->tid)->pluck('name');
						$recent_reply[$key]['time']=$this->time_format($value['time']);
						$recent_reply[$key]['reply_user']=User::where('id',$value['reply_uid'])->pluck('name');
					}
					$result->data['recent_replay']=$recent_reply;
					//参与和发起的接口
				}else{	
					//发起的接口
					if($isPreview=='topics'){
						$result=TiezeDetail::select('preview_content','comment','id','tid','time')->where('uid',$uid)->where('parent',0)->orderBy('time','desc')->simplePaginate(12);
						foreach ($result as $key => $value) {
							$result[$key]['partQz']=Tieze::where('id',$value['tid'])->pluck('name');
							$result[$key]['time']=$this->time_format($value['time']);
						}
					}else{//参与的接口
						$result=TiezeDetailComment::where('uid',$uid)->select('preview_content','time','tid','reply_uid')->orderBy('time','desc')->simplePaginate(8);
						foreach ($result as $key => $value) {
							//取到贴子id
							$tiezeInfo=TiezeDetail::where('id',$value['tid'])->select('tid','parent','preview_content','comment')->first();
							$result[$key]['comment']=$tiezeInfo->comment;
							$result[$key]['id']=$value['tid'];
							$result[$key]['tid']=$tiezeInfo->tid;
							//存在上级标识是副贴
							if($tiezeInfo->parent){
								$result[$key]['content_child']=$tiezeInfo->preview_content;
								$result[$key]['content']=TiezeDetail::where('id',$tiezeInfo->parent)->pluck('preview_content');
							}else{	
								$result[$key]['content']=$tiezeInfo->preview_content;
								$result[$key]['content_child']='';
							}
							//取到贴子名字
							$result[$key]['partQz']=Tieze::where('id',$tiezeInfo->tid)->pluck('name');
							$result[$key]['time']=$this->time_format($value['time']);
							$result[$key]['reply_user']=User::where('id',$value['reply_uid'])->pluck('name');
						}
					}
					if($uid=$request->input('uid')){
						$userInfo=User::select('userImg','name')->find($uid);
						foreach ($result as $key => $value) {
							$result[$key]['userImg']=$userInfo['userImg'];
							$result[$key]['name']=$userInfo['name'];
						}
					}
				}
			}
			return $result->toJson();
		}
		public function isImage($fileName){
			//能得到相关信息，就代表是图片
			if(@getimagesize($fileName)){
				return true;
			}else{
				return false;
			}
		}
		public function uploadImage(Request $request,$uploadPath="zcjStore",$maxsize=2097152,$allowExt=array('jpeg','png','gif','jpg')){
			//上传类型 是否为用户关像
			$isUserImg=false;
			if(array_key_exists('userImg',$_FILES)){
				$isUserImg=true;
				$info=explode("/",$_FILES['userImg']['type']);
			}else{
				// 0是扩展名，1是accesctoken,2是所属的窝子id，3是贴子内容,在第一个file对象中获取
				$info=explode("/",$_FILES[0]['type']);
				$tid=$info[2];
				$content=strip_tags(urldecode($info[3]));
				$img=[];
			}
			if($uid=$this->checkLogin($info[1])){
				$result=new Result;
				if(!$isUserImg){
					//贴子图片上传
					foreach ($_FILES as $key => $fileInfo) {
						//上传错误
						if($fileInfo['error']!=0){
							$result->status=0;
							$result->message='上传错误';
							return $result->toJson();
						}
						// //判段上传的类型
						$ext=explode("/",$fileInfo['type'])[0];
						//不允许的类型，你吃屎吧
						if(!in_array($ext,$allowExt)){
							$result->status=0;
							$result->message='上传文件类型非法';
							return $result->toJson();
						}
						//大于自定义大小的文件，给我去死
						if($fileInfo['size']>$maxsize){
							$result->status=0;
							$result->message='文件大于2m';
							return $result->toJson();
						}
						//检测服务端保存上传的临时文件是否是图片
						if(!$this->isImage($fileInfo['tmp_name'])){
							$result->status=0;
							$result->message='文件不是真实图片';
							return $result->toJson();
						}
						//检测服务端保存目录是否存在,不存在就创建
						if(!file_exists($uploadPath)){
							//不存在的操作,false
							mkdir($uploadPath,755,true);
						}
						//文件名拼上上传后缀名
						//这里不填参数了，微秒也以经够用了
						$uniName=$uid.';'.uniqid().'.'.$ext;
						$destination=$uploadPath.'/'.$uniName;
						//把服务器端中的上传临时文件移动到指定目录
						if(!@move_uploaded_file($fileInfo['tmp_name'],$destination)){
							$result->status=0;
							$result->message='系统错误';
							return $result->toJson();
						}
						$url=$this->imageBasePath.$destination;
						array_push($img,$url);
					}
					$tiezeDetailDb=new TiezeDetail;
					$tiezeDetailDb->img=implode(',',$img);
					$tiezeDetailDb->uid=$uid;
					$tiezeDetailDb->tid=$tid;
					$tiezeDetailDb->time=time();
					$tiezeDetailDb->content=$content;
					if(strlen($content)>=50){
						$preview_content=substr($content,0,100);
						$tiezeDetailDb->preview_content=$preview_content;
					}else{
						$tiezeDetailDb->preview_content=$content;
					}
					$tiezeDetailDb->save();
					Tieze::where('id',$tid)->increment('comment');			
				}else{
					//用户头像上传
					//上传错误
					$fileInfo=$_FILES['userImg'];
					if($fileInfo['error']!=0){
						$result->status=0;
						$result->message='上传错误';
						return $result->toJson();
					}
					// //判段上传的类型
					$ext=explode("/",$fileInfo['type'])[0];
					//不允许的类型，你吃屎吧
					if(!in_array($ext,$allowExt)){
						$result->status=0;
						$result->message='上传文件类型非法';
						return $result->toJson();
					}
					//大于自定义大小的文件，给我去死
					if($fileInfo['size']>$maxsize){
						$result->status=0;
						$result->message='文件大于2m';
						return $result->toJson();
					}
					//检测服务端保存上传的临时文件是否是图片
					if(!$this->isImage($fileInfo['tmp_name'])){
						$result->status=0;
						$result->message='文件不是真实图片';
						return $result->toJson();
					}
					//检测服务端保存目录是否存在,不存在就创建
					if(!file_exists($uploadPath)){
						//不存在的操作,false
						mkdir($uploadPath,755,true);
					}
					//文件名拼上上传后缀名
					//这里不填参数了，微秒也以经够用了
					$uniName=$uid.';'.uniqid().'.'.$ext;
					$destination=$uploadPath.'/'.$uniName;
					//把服务器端中的上传临时文件移动到指定目录
					if(!@move_uploaded_file($fileInfo['tmp_name'],$destination)){
						$result->status=0;
						$result->message='系统错误';
						return $result->toJson();
					}
					$user=User::find($uid);
					if($user->userImg!='http://47.95.9.204:9501/images/default_user.png'){
						$deletePath=explode('zcjStore/',$user->userImg)[1];
						unlink('zcjStore/'.$deletePath);
					}
					$user->userImg=$this->imageBasePath.$destination;
					$user->save();
					$result->status=1;
					$result->data=$user->userImg;
					return $result->toJson();
				}
				$result->status=1;
				$result->data='上传成功!';
				return $result->toJson();
			}
		}
		public function getTiezeData(Request $request){
			$tid=$request->input('tid','');
			//tid存在就代表是一个窝子的贴子
			if($tid){
				$order=$request->input('order','');
				if($order=='hot'){
					//最热
					$result=TiezeDetail::select('id','preview_content','goods','comment','watch','uid','img','time','tid')->where('parent',0)->where('tid',$tid)->orderBy('comment','desc')->simplePaginate(8);
				}else{
					//最新
					$result=TiezeDetail::select('id','preview_content','goods','comment','watch','uid','img','time','tid')->where('parent',0)->where('tid',$tid)->orderBy('time','desc')->simplePaginate(8);
				}
			}else{
				//数据应譔要有些算法推贱啥的，现在没数据。。。
				$result=TiezeDetail::select('id','preview_content','goods','comment','watch','uid','img','time','tid')->where('parent',0)->simplePaginate(8);
			}
			foreach ($result as $key => $value) {
				$userInfo=User::select('name','userImg')->where('id',$value['uid'])->first();
				//只操作data
				$result[$key]->name=$userInfo->name;
				$result[$key]->userImg=$userInfo->userImg;
				$result[$key]->time=$this->time_format($value['time']);
				$result[$key]->img=explode(',',$value['img']);
				//我是用utfmb4编码 mysql5.7 preview_content字段为char(50)
				//全中文也是存50位，全英文也是存50位，但是可以多一个?字符，就变成51位了
				if(strlen($value['preview_content'])>=50){
					$result[$key]->moreFlag=true;
				}else{
					$result[$key]->moreFlag=false;
				}
				$result[$key]->partQz=Tieze::where('id',$value['tid'])->pluck('name');
			}
			return $result->toJson();
		}
		public function getWisdom(Request $request){
			return Wisdom::where('id',mt_rand(1,15))->pluck('wisdom');
		}
		public function getTiezeInfo(Request $request){
			return Tieze::find($request->input('tid'));
		}
		public function getTiezeDetail(Request $request){
			if($request->input('isComment','')){
				$result=TiezeDetail::select('id','content','goods','comment','watch','uid','img','time','tid','parent')->find($request->input('tid'));
			}else{
				$result=TiezeDetail::select('id','content','goods','comment','watch','uid','img','time','tid')->find($request->input('tid'));
			}
			$userInfo=User::select('name','userImg')->where('id',$result['uid'])->first();
			//只操作data
			$result->name=$userInfo->name;
			$result->userImg=$userInfo->userImg;
			if($uid=$this->checkLogin($request->input('access_token'))){
				$result->mygood=GoodTieze::where('uid',$uid)->where('tid',$result->id)->where('type',0)->pluck('uid');
			}else{
				$result->mygood=0;
			}
			$result->time=$this->time_format($result['time']);
			$result->img=explode(',',$result['img']);
			$result->partQz=Tieze::where('id',$result['tid'])->pluck('name');	
			return $result->toJson();
		}
		public function getTiezeDetailComment(Request $request){
			$result=TiezeDetail::select('id','content','uid','time','parent','tid','goods','commentId')->where('parent',$request->input('tid'))->orderby('time','desc')->simplePaginate(8);
			$uid=$this->checkLogin($request->input('access_token'));			
			//存在这个tieze标识的话
			if($request->input('type')=='tieze'){
				$data=$result->toArray()['data'];
				foreach ($data as $key => $value) {
					$userInfo=User::select('name','userImg')->where('id',$value['uid'])->first();
					if($uid){
						$data[$key]['mygood']=GoodTieze::where('uid',$uid)->where('tid',$data[$key]['id'])->where('type',0)->pluck('uid');
					}else{
						$data[$key]['mygood']=0;
					}
					//只操作data
					$data[$key]['name']=$userInfo->name;
					$data[$key]['userImg']=$userInfo->userImg;
					$data[$key]['time']=$this->time_format($value['time']);
					$comment=TiezeDetailComment::select('id','reply','uid','goods','time')->where('tid',$value['id'])->get();
					//有坑还要不要譔要查回复的。。。想错了。。。
					foreach ($comment as $keyd => $va) {
						$userInfo=User::select('name','userImg')->where('id',$va['uid'])->first();
						$comment[$keyd]->name=$userInfo->name;
						if($uid){
							$comment[$keyd]->mygood=GoodTieze::where('uid',$uid)->where('tid',$va[$keyd]['id'])->where('type',1)->pluck('uid');
						}else{
							$comment[$keyd]->mygood=0;
						}
						$comment[$keyd]->time=$this->time_format($va['time']);
						$comment[$keyd]->userImg=$userInfo->userImg;
					}
					//直接操作simplagePaginate对象的data属性
					$result[$key]=['main'=>$data[$key],'comment'=>$comment->toArray()];
				}
			}else{
				foreach ($result as $key => $value){
					$userInfo=User::select('name','userImg')->where('id',$value['uid'])->first();
					if($uid){
						$result[$key]['mygood']=GoodTieze::where('uid',$uid)->where('tid',$result[$key]['id'])->where('type',1)->pluck('uid');
					}else{
						$result[$key]['mygood']=0;
					}
					//只操作data
					$result[$key]->name=$userInfo->name;
					$result[$key]->userImg=$userInfo->userImg;
					$result[$key]->time=$this->time_format($value['time']);
					$result[$key]->partQz=Tieze::where('id',$value['tid'])->pluck('name');
				}
			}
			return $result->toJson();
		}
		public function goodTieze(Request $request){
			$access_token=$request->input('access_token','');
			$isDel=$request->input('isDel','');
			$gid=$request->input('gid','');
			$uid=$this->checkLogin($access_token);
			$pushMessageNum=new PushMessageNum($gid);
			if($uid){
				$type=$request->input('type','');
				$tid=$request->input('tid','');
				User::where('id',$gid)->increment('goods');
				$goodTiezeDb=new GoodTieze();
				//点赞了贴子或者副贴
				if($type=='detail'){
					if($isDel){//取消点赞
						GoodTieze::where('uid',$uid)->where('tid',$tid)->where('type',0)->delete();
						TiezeDetail::where('id',$tid)->decrement('goods');
						User::where('id',$gid)->decrement('goods');
						//是否是副贴， 如果是副贴的话还要对评论表进行操作
						if($cid=$request->input('isAssistant','')){	
							GoodTieze::where('uid',$uid)->where('tid',$cid)->where('type',1)->delete();
							TiezeDetailComment::where('id',$tid)->decrement('goods');
						}
						$pushMessageNum->editPushMessageNum('good','delete');
					}else{
						$goodTiezeDb=new GoodTieze;
						$goodTiezeDb->uid=$uid;
						$goodTiezeDb->tid=$tid;
						$goodTiezeDb->gid=$gid;
						$goodTiezeDb->time=time();	
						$goodTiezeDb->save();
						TiezeDetail::where('id',$tid)->increment('goods');
						$pushMessageNum->editPushMessageNum('good','add');
					}
					return ['status'=>1];
				}else{//点赞了评论
					if($isDel){//取消点赞评论
						GoodTieze::where('uid',$uid)->where('tid',$tid)->where('type',1)->delete();
						TiezeDetailComment::where('id',$tid)->decrement('goods');
						User::where('id',$gid)->decrement('goods');
						$pushMessageNum->editPushMessageNum('good','delete');
					}else{
						$goodTiezeDb->uid=$uid;
						$goodTiezeDb->tid=$tid;
						$goodTiezeDb->gid=$gid;
						$goodTiezeDb->time=time();
						$goodTiezeDb->type=1;
						$goodTiezeDb->save();
						TiezeDetailComment::where('id',$tid)->increment('goods');
						$pushMessageNum->editPushMessageNum('good','add');
					}
					return ['status'=>1];
				}
			}
		}
		public function getTiezeDetailCommentComment(Request $request){
			$tid=$request->input('tid','');
			$access_token=$request->input('access_token','');
			$comment=TiezeDetailComment::select('id','reply','uid','goods','time')->where('tid',$tid)->get();
			$uid=$this->checkLogin($access_token);
			foreach ($comment as $keyd => $va) {
				$userInfo=User::select('name','userImg')->where('id',$va['uid'])->first();
				$comment[$keyd]->name=$userInfo->name;
				if($uid){
					$comment[$keyd]->mygood=GoodTieze::where('uid',$uid)->where('tid',$va['id'])->where('type',1)->pluck('uid');
				}else{
					$comment[$keyd]->mygood=0;
				}
				$comment[$keyd]->time=$this->time_format($va['time']);
				$comment[$keyd]->userImg=$userInfo->userImg;
			}
			return $comment->toJson();
		}
		public function sendTieze(Request $request){
			if($uid=$this->checkLogin($request->input('access_token',false))){
				$tid=$request->input('tid','');
				$content=htmlspecialchars($request->input('content',''));
				$parent=$request->input('parent','');
				//纯文字主贴接口
				$tiezeDetailDb=new TiezeDetail;
				$tiezeDetailDb->uid=$uid;
				$tiezeDetailDb->tid=$tid;
				$tiezeDetailDb->time=time();
				$tiezeDetailDb->content=$content;
				if(strlen($content)>=50){
					$preview_content=substr($content,0,100);
					$tiezeDetailDb->preview_content=$preview_content;
				}else{
					$tiezeDetailDb->preview_content=$content;
					//return $tiezeDetailDb->preview_content;
				}
				//主贴回复接口,也就是副贴
				if($parent){
					$tiezeDetailDb->parent=$parent;		
					//要往评论表插入数据
					$commentDb=new TiezeDetailComment;	
					$commentDb->uid=$uid;
					$commentDb->reply=$content;
					if(strlen($content)>=50){
						$preview_content=substr($content,0,100);
						$commentDb->preview_content=$preview_content;
					}else{
						$commentDb->preview_content=$content;
					}
					$commentDb->reply_uid=$request->input('rid','');
					$commentDb->tid=$parent;
					$commentDb->time=time();
					$commentDb->save();
					$tiezeDetailDb->commentId=$commentDb->id;		
					//贴子的回复数加1
					TiezeDetail::where('id',$parent)->increment('comment');	
					//通知主贴作者
					//msyql
					$replyDb=new Reply;
					$rid=$request->input('rid');	
					$replyDb->send=$rid;
					$replyDb->cid=$commentDb->id;
					$replyDb->save();
					//redis
					 $pushMessageNum=new PushMessageNum($rid);
					$pushMessageNum->editPushMessageNum('reply','add');
				}else{
					//所在窝子的发贴数加1
					Tieze::where('id',$tid)->increment('comment');			
				}
				$tiezeDetailDb->save();
				return ['status'=>$tiezeDetailDb->id,'message'=>$tiezeDetailDb->time];
			}
		}
		public function sendComment(Request $request){
			if($uid=$this->checkLogin($request->input('access_token',false))){
				$tid=$request->input('tid','');
				$content=htmlspecialchars($request->input('content',''));
				$rid=$request->input('rid','');
				$replyId=$request->input('replyId','');
				$replyUserName=$request->input('replyUserName','');
				if($replyUserName){
					$content=$replyUserName.$content;					
				}
				//要往评论表插入数据
				$commentDb=new TiezeDetailComment;	
				$commentDb->uid=$uid;
				$commentDb->reply=$content;
				if(strlen($content)>=50){
					$preview_content=substr($content,0,100);
					$commentDb->preview_content=$preview_content;
				}else{
					$commentDb->preview_content=$content;
				}
				$commentDb->reply_uid=$replyId?$replyId:$rid;
				$commentDb->tid=$tid;
				$commentDb->time=time();
				$commentDb->save();
				TiezeDetail::where('id',$tid)->increment('comment');
				//通知副贴作者如果还回复了其他人，还要通知其他人
				//msyql 副贴作者
				$replyDb=new Reply;
				$replyDb->send=$rid;
				$replyDb->cid=$commentDb->id;
				$replyDb->save();
				//redis
				$pushMessageNum=new PushMessageNum($rid);
				$pushMessageNum->editPushMessageNum('reply','add');
				//其他人也存在的话就要通知了呀
				if($replyId){
					$replyDb=new Reply;
					$replyDb->send=$replyId;
					$replyDb->cid=$commentDb->id;
					$replyDb->save();
					//redis
					$pushMessageNum=new PushMessageNum($replyId);
					$pushMessageNum->editPushMessageNum('reply','add');
				}
				return ['status'=>$commentDb->id];
			}
		}
		public function getReplyData(Request $request){
			$access_token=$request->input('access_token');
			if($uid=$this->checkLogin($access_token)){
				$replyDb=new Reply;
				$fromUidArr=$replyDb->where('send',$uid)->select('cid')->get();
				$result=TiezeDetailComment::whereIn('id',$fromUidArr)->select('preview_content','time','tid','uid')->orderBy('time','desc')->simplePaginate(8);
				foreach ($result as $key => $value) {
					$tiezeInfo=TiezeDetail::where('id',$result[$key]['tid'])->select('parent','preview_content')->first();
					//存在上级标识是副贴
					if($tiezeInfo->parent){
						$result[$key]['isMain']=false;
					}else{	
						$result[$key]['isMain']=true;
					}
					$result[$key]['content']=$tiezeInfo->preview_content;
					$result[$key]['time']=$this->time_format($value['time']);
					$result[$key]['user']=User::where('id',$value['uid'])->select('name','userImg')->first();
				}
			}
			return $result->toJson();
		}
		public function getLikeData(Request $request){
			$access_token=$request->input('access_token');
			if($uid=$this->checkLogin($access_token)){
				$likeMeData=GoodTieze::where('gid',$uid)->orderby('time','desc')->simplePaginate(8);
				foreach($likeMeData as $key=>$value){
					$userInfo=User::where('id',$value['uid'])->select('name','userImg')->first();
					$likeMeData[$key]->name=$userInfo->name;
					$likeMeData[$key]->userImg=$userInfo->userImg;
					$likeMeData[$key]->uid=$value['uid'];
					$likeMeData[$key]->time=$this->time_format($value['time']);
					if($value['type']==0){
						$likeMeData[$key]->content=TiezeDetail::where('id',$value['tid'])->pluck('preview_content');
					}else{
						$likeMeData[$key]->content=TiezeDetailComment::where('id',$value['tid'])->pluck('preview_content');
					}
				}
				return $likeMeData;
			}
		} 
		public function getFollowData(Request $request){
			$access_token=$request->input('access_token');
			if($uid=$this->checkLogin($access_token)){
				$mode=$request->input('mode','');
				$fid=[];
				//我关注的
				if(!$mode){
					$followUserData=Follow::where('uid',$uid)->select('fid')->simplePaginate(8);
					foreach($followUserData as $key=>$value){
						array_push($fid,$value['fid']);
					}
				}else{
					//关注我的
					$followUserData=Follow::where('fid',$uid)->select('uid')->simplePaginate(8);
					foreach($followUserData as $key=>$value){
						array_push($fid,$value['uid']);
					}
				}
				//查出用户的信息
				$followUser=User::whereIn('id',$fid)->select('id','name','summary','userImg','goods','fans')->get();
				foreach($followUserData as $key=>$value){
					$followUserData[$key]->uid=$followUser[$key]->id;
					$followUserData[$key]->userName=$followUser[$key]->name;
					$followUserData[$key]->summary=$followUser[$key]->summary;
					$followUserData[$key]->userImg=$followUser[$key]->userImg;
					$followUserData[$key]->fans=$followUser[$key]->fans;
					$followUserData[$key]->goods=$followUser[$key]->goods;
				}
				return $followUserData;				
			}
		}
		public function followUser(Request $request){
			$access_token=$request->input('access_token');
			if($uid=$this->checkLogin($access_token)){
				$fid=$request->input('fid');
				$isDel=$request->input('isDel');
				//真 为取消关注
				if($isDel){
					Follow::where('uid',$uid)->where('fid',$fid)->delete();
					User::where('id',$fid)->decrement('fans');
					$pushMessageNum=new PushMessageNum($fid);
					$pushMessageNum->editPushMessageNum('followMy','delete');
					return ['status'=>'取消成功'];
				}else{
					$follow=new Follow;
					$follow->uid=$uid;
					$follow->fid=$fid;
					$follow->save();
					User::where('id',$fid)->increment('fans');
					User::where('id',$uid)->increment('follow');
					//推送给被关注的人
					$pushMessageNum=new PushMessageNum($fid);
					$pushMessageNum->editPushMessageNum('followMy','add');
					return ['status'=>'关注成功'];
				}
			}
		}
		public function setZero(Request $request){
			if($uid=$this->checkLogin($request->input('access_token'))){
				$type=$request->input('type');
				$pushMessageNum=new PushMessageNum($uid);
				$pushMessageNum->editPushMessageNum($type,'deleteAll');
			}	
		}
		public function followTieze(Request $request){
			if($uid=$this->checkLogin($request->input('access_token'))){
				$tid=$request->input('tid');
				$userFollowTiezeId=User::find($uid);
				$userFollowTiezeIdArr=explode(',',$userFollowTiezeId->followtieze);
				if(in_array($tid,$userFollowTiezeIdArr)){
					return ['status'=>2];
				}else{
					array_push($userFollowTiezeIdArr,$tid);
					$userFollowTiezeId->followTieze=implode(',',$userFollowTiezeIdArr);
					$userFollowTiezeId->save();
					return ['status'=>1];
				}
			}
		}
		public function tiezePush(Request $request){
			switch ($request->input('type')) {
				case '0':
					$arr1=[2,3,4,5];
					$arr2=[6,7,9,8];
					$arr3=[10,1,12,11];
					$arr4=[13,14,15,16];
					$arr5=[17,18];
					$mode=mt_rand(1,5);
					switch ($mode) {
						case 1:
							$arr=$arr1;
						break;
						case 2:
							$arr=$arr2;
						break;
						case 3:
							$arr=$arr3;
						break;
						case 4:
							$arr=$arr4;
						break;
						case 5:
							$arr=$arr5;
						break;
					}
					return Tieze::whereIn('id',$arr)->get();
				break;	
				case '1':
					return Tieze::where('type',0)->get();
				break;
				case '2':
					return Tieze::where('type',1)->get();
				break;			
				case '3':
					return Tieze::where('type',2)->get();
				break;			
				default:
					break;
			}
		}
		public function saveSummary(Request $request){
			if($uid=$this->checkLogin($request->input('access_token'))){
				$summary=$request->input('summary');
				$user=User::find($uid);
				$user->summary=$summary;
				$user->save();
				return ['summary'=>$summary];
			}else{
				return $request->input('access_token');
			}
		}
		public function search(Request $request){
			$type=$request->input('index');
			$data=$request->input('content');
			switch ($type) {
				//查找圈子
				case 0:
					return Tieze::where('name','like','%'.$data.'%')->get();
				break;
				//查找贴子
				case 1:
					$result=TiezeDetail::where('content','like','%'.$data.'%')->select('id','parent','preview_content')->get();
					foreach ($result as $key=>$value){
						if($parent=$result[$key]->parent){
							$result[$key]->id=$parent;
						}
					}
					return $result;
				//查找用户
				case 2:					
					$result=User::where('name','like','%'.$data.'%')->get();
					$uid=$this->checkLogin($request->input('access_token'));
					$uidArr=[];
					foreach($result as $key=>$value){
						$result[$key]->password=null;
						array_push($uidArr,$value['id']);
					}
					//与用户的关注关系
					foreach($uidArr as $key=>$value){
						$result[$key]->isFollow=false;
						if($uid){
							if(Follow::where('uid',$uid)->where('fid',$value)->pluck('uid')){
								$result[$key]->isFollow=true;
							}
						}
					}
					return $result;
			}
		}
		public function imageCodeToken(Request $request){
			$validateCode=new validateCode();
			$code=$validateCode->createCode();
			$redis=new Redis();
        	$redis->pconnect('localhost', 6379);
        	$token=md5(uniqid(mt_rand(), true));
        	$redis->set("img".$token,$code);
			return "img".$token;
		}
		public function register(Request $request){
			$name=$request->input('name');
			$password=$request->input('password');
			$verifyCode=$request->input('verifyCode');
			$redis=new Redis();
        	$redis->pconnect('localhost', 6379);
			$imageCodeToken=$request->input('imageCodeToken');
			if(User::where('name',$name)->pluck('id')){
				return ['status'=>1,'message'=>'用户名己使用'];
			}
			if(strtolower($verifyCode)!=strtolower($redis->get($imageCodeToken))){
				return ['status'=>1,'message'=>'验证码错误'];
			}
			$user=new User;
			$user->name=$name;
			$user->password=password_hash($password, PASSWORD_DEFAULT);
			$user->save();
			$access_token=md5($user->password.mt_rand());
			//存储access_token
			$UserHasLoginRedis=new UserHasLoginRedis($access_token,[$user->id,time()]);
			$redis->set($user->id,$access_token);
			return ['status'=>0,'message'=>'注册成功','access_token'=>$access_token];
		}
		public function login(Request $request){
			$name=$request->input('name');
			$password=$request->input('password');
			$user=User::where('name',$name)->select('password','id')->first();
			if(isset($user->id)){
				if(password_verify($password,$user->password)){
					$redis=new Redis();
		        	$redis->pconnect('localhost', 6379);
					//删除用户之前的access_token
					$oldToken=$redis->get($user->id);
					$redis->delete($oldToken);
		        	//重新设置access_token的生效时间
					$access_token=md5($user->password.mt_rand());
					//存储access_token
					$UserHasLoginRedis=new UserHasLoginRedis($access_token,[$user->id,time()]);
					$redis->set($user->id,$access_token);
					return ['status'=>0,'message'=>'登录成功','access_token'=>$access_token];
				}else{
					return ['status'=>1,'message'=>'密码错误'];
				}
			}else{
				return ['status'=>1,'message'=>'此用户不存在'];
			}

		}
		
	}

 ?>

